package com.aier.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Component;

import com.aier.admin.service.IAdminEntranceService;
import com.aier.common.dao.ICommonDAO;
import com.aier.entrance.entity.EntranceDevice;
import com.aier.entrance.entity.EntranceDeviceRowMapper;
import com.aier.entrance.service.IEntranceInitCommonService;

@Component("adminEntranceService")
public class AdminEntranceServiceImpl_J implements IAdminEntranceService {

    @Resource(name = "commonDao")
    private ICommonDAO commonDao;
    @Resource(name="entranceInitCommonService")
    private IEntranceInitCommonService entranceInitCommonService;

    public void setCommonDao(ICommonDAO commonDao) {
        this.commonDao = commonDao;
    }

    @Override
    public int insertEntrance(EntranceDevice entranceDevice) {
        if (entranceDevice.getState() != null) {
            EntranceDevice en = new EntranceDevice();
            en.setSn(entranceDevice.getSn());
            en.setState(entranceDevice.getState());
            en.setDes(entranceDevice.getDes());

            en.setVdstate(entranceDevice.getVdstate());
            en.setSupplier(entranceDevice.getSupplier());
            en.setPoxystate(IAdminEntranceService.POXYSTATE_NULL);
            commonDao.insert(
                    EntranceDeviceRowMapper.getPreparedStatementCreator(en),
                    new GeneratedKeyHolder());
            entranceInitCommonService.initEntrance();
            return 1;
        } else {
            return 0;
        }
    }

    // 显示信息
    @Override
    public Map<String, Object> searchEntranceDevice(int page, int page_size) {
        StringBuffer sql = new StringBuffer("select * from entrance_device");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new EntranceDeviceRowMapper());

    }

    // 删除信息
    @Override
    public int updateEntranceByid(Long eid) {
        if (commonDao.update("delete from entrance_device where eid=?",
                new Object[] { eid }) > 0) {
            entranceInitCommonService.initEntrance();
            return 1;
        } else {
            return 0;
        }
    }

    // 修改信息
    @Override
    public int updateByeid(Long eid, String sn, Short state, String des,
            Short vdstate, Short supplier) {

        if (commonDao
                .update("update entrance_device e set e.sn=?,e.state=?,e.des=?,e.vdstate=?,e.supplier=? where e.eid=?",
                        new Object[] { sn, state, des, vdstate, supplier, eid }) > 0) {
            entranceInitCommonService.initEntrance();
            return 1;
        } else {
            return 0;
        }
    }

    // 根据id查询某条门禁记录
    @Override
    public EntranceDevice searchByeid(Long eid) {
        return (EntranceDevice) commonDao.listByObject(
                "select * from entrance_device where eid=?",
                new Object[] { eid }, new EntranceDeviceRowMapper());
    }

    // 根据id分派门禁设备
    @Override
    public int updatepidByeid(Integer pid, Long eid) {
        short poxystate=IAdminEntranceService.POXYSTATE_PROXY;
        if(null==pid){
            poxystate=IAdminEntranceService.POXYSTATE_NULL;
        }
        if (commonDao.update(
                "update entrance_device e set e.pid=?,e.poxystate=? where e.eid=?",
                new Object[] { pid,poxystate, eid }) > 0) {
            entranceInitCommonService.initEntrance();
            return 1;
        } else {
            return 0;
        }
    }

    //查出所有未分配的门禁设备
    @Override
    public Map<String, Object> selectUnassign(int page, int page_size) {
        StringBuffer sql = new StringBuffer(
                "select * from entrance_device where pid is  null");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new EntranceDeviceRowMapper());

    }
    //查出所有已分配的门禁设备
    @Override
    public Map<String, Object> selectAlreadyassign(int page, int page_size) {
        StringBuffer sql = new StringBuffer(
                "select * from entrance_device where pid is not null");
        List<Object> lo = new ArrayList<Object>();
        return commonDao.queryPage(sql.toString(), page, page_size, lo,
                new EntranceDeviceRowMapper());
    }

    public void setEntranceInitCommonService(
            IEntranceInitCommonService entranceInitCommonService) {
        this.entranceInitCommonService = entranceInitCommonService;
    }

}
